package com.eliteams.quick4j.web.app.wechat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @FileName DownloadMedia.java
 * @Description 媒体文件下载
 * @Version 1.0
 * @Author liugy.
 * @Email 958130364@qq.com
 * @History <br>
 * <li>Author: liugy</li>
 * <li>Date: 2017-02-15</li>
 * <li>Version: 1.0</li>
 * <li>Content: create</li>
 */
public class DownloadMedia {
	
	/** 普通日志 */
	private static final Logger	logger			= LoggerFactory.getLogger(DownloadMedia.class);
												
	/** 素材下载:不支持视频文件的下载(GET) */
	private static final String	DOWNLOAD_MEDIA	= "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s";
												
	public static String getDownloadUrl(String token, String mediaId) {
		return String.format(DOWNLOAD_MEDIA, token, mediaId);
	}
	
	/**
	 * 多媒体下载接口
	 * @comment 不支持视频文件的下载
	 * @param fileName  素材存储文件路径
	 * @param token     认证token
	 * @param mediaId   素材ID（对应上传后获取到的ID）
	 * @return 素材文件
	 */
	public static File downloadMedia(String fileName, String token, String mediaId) {
		String url = getDownloadUrl(token, mediaId);
		return httpRequestToFile(fileName, url, "GET", null);
	}
	
	/**
	 * 以http方式发送请求,并将请求响应内容输出到文件
	 * @param path    请求路径
	 * @param method  请求方法
	 * @param body    请求数据
	 * @return 返回响应的存储到文件
	 */
	public static File httpRequestToFile(String fileName, String path, String method, String body) {
		if (fileName == null || path == null || method == null) {
			return null;
		}
		
		File file = null;
		HttpURLConnection conn = null;
		InputStream inputStream = null;
		FileOutputStream fileOut = null;
		try {
			URL url = new URL(path);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(method);
			if (null != body) {
				OutputStream outputStream = conn.getOutputStream();
				outputStream.write(body.getBytes("UTF-8"));
				outputStream.close();
			}
			
			inputStream = conn.getInputStream();
			if (inputStream != null) {
				file = new File(fileName);
			} else {
				return file;
			}
			
			//写入到文件
			fileOut = new FileOutputStream(file);
			if (fileOut != null) {
				int c = inputStream.read();
				while (c != -1) {
					fileOut.write(c);
					c = inputStream.read();
				}
			}
		} catch (Exception e) {
			logger.error(String.valueOf(e));
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
			
			/*
			 * 必须关闭文件流
			 * 否则JDK运行时，文件被占用其他进程无法访问
			 */
			try {
				inputStream.close();
				fileOut.close();
			} catch (IOException execption) {
				logger.error(String.valueOf(execption));
			}
		}
		return file;
	}
	
}
